package com.data.modules.demo.biz.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.data.common.api.Insert;
import com.data.common.api.Update;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.aspect.annotation.DataAuditLogging;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.modules.demo.biz.entity.Demo;
import com.data.modules.demo.biz.service.IDemoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: demo
 * @Author: data-boot
 * @Date:   2021-12-22
 * @Version: V1.0
 */
 @SuppressWarnings("ALL")
@Api(tags="demo")
@RestController
@RequestMapping("/biz/demo")
@Slf4j
public class DemoController extends BootController<Demo, IDemoService> {
	@Autowired
	private IDemoService demoService;
	@Autowired
	@Lazy
	private ISysUserService sysUserBiz;
	/**
	 * 分页列表查询
	 *
	 * @param demo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "demo-分页列表查询")
	@ApiOperation(value="demo-分页列表查询", notes="demo-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Demo demo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Demo> queryWrapper = QueryGenerator.initQueryWrapper(demo, req.getParameterMap());
		Page<Demo> page = new Page<Demo>(pageNo, pageSize);
		IPage<Demo> pageList = demoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param demo
	 * @return
	 */
	@AutoLog(value = "demo-添加")
	@ApiOperation(value="demo-添加", notes="demo-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody @Validated(Insert.class) Demo demo) {
		demoService.save(demo);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param demo
	 * @return
	 */
	@DataAuditLogging
	@AutoLog(value = "demo-编辑")
	@ApiOperation(value="demo-编辑", notes="demo-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody @Validated(Update.class) Demo demo) {
		demoService.updateById(demo);
		return Result.OK("编辑成功!");
	}
	 @DataAuditLogging(tag = "editTest")
	 @AutoLog(value = "demo-editTest")
	 @ApiOperation(value="demo-editTest", notes="demo-editTest")
	 @PutMapping(value = "/editTest")
	 public Result<?> editTest(String username,String orgCode) {
		 sysUserBiz.updateUserDepart(username,orgCode);
		 return Result.OK("编辑成功!");
	 }
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "demo-通过id删除")
	@ApiOperation(value="demo-通过id删除", notes="demo-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		demoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "demo-批量删除")
	@ApiOperation(value="demo-批量删除", notes="demo-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.demoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "demo-通过id查询")
	@ApiOperation(value="demo-通过id查询", notes="demo-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Demo demo = demoService.getById(id);
		if(demo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(demo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param demo
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Demo demo) {
        return super.exportXls(request, demo, Demo.class, "demo");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Demo.class);
    }


	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "电话查重API")
	 @ApiOperation(value="电话查重API", notes="电话查重")
	 @GetMapping(value = "/resumeIsExist")
	 public Result<?> resumeIsExist(@RequestParam(name="phone",required=true) String phone) {
		 String str = demoService.resumeIsExist(phone);
		 if(str==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(str);
	 }


}
